package com.ruoyi.message.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.exception.BaseException;
import com.ruoyi.common.core.plus.web.controller.BasePlusController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.message.general.entity.MessageCountryTemplate;
import com.ruoyi.message.general.entity.MessageTemplate;
import com.ruoyi.message.manage.service.ManageMessageCountryTemplateService;
import com.ruoyi.message.manage.service.ManageMessageTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message/messageCountryTemplate")
@Api(tags = "消息国家模板绑定")
public class ManageMessageCountryTemplateController extends BasePlusController {

    @Autowired
    private ManageMessageCountryTemplateService manageMessageCountryTemplateService;
    @Autowired
    private ManageMessageTemplateService manageMessageTemplateService;

    @GetMapping("/{templateId}")
    @ApiOperation(value = "查看")
    @PreAuthorize("@ss.hasPermi('message:message_template:query')")
    public AjaxResult getInfo(@PathVariable Long templateId) {
        LambdaQueryWrapper<MessageCountryTemplate> messageCountryTemplateLambdaQueryWrapper = Wrappers.lambdaQuery(MessageCountryTemplate.class)
                .eq(MessageCountryTemplate::getTemplateId, templateId);
        List<MessageCountryTemplate> messageCountryTemplateList = manageMessageCountryTemplateService.getByList(messageCountryTemplateLambdaQueryWrapper);
        return AjaxResult.success(messageCountryTemplateList);
    }

    @PostMapping(value = "/{templateId}")
    @ApiOperation(value = "模板绑定国家")
    @PreAuthorize("@ss.hasPermi('message:message_template:edit')")
    public AjaxResult templateBindingCountry(@PathVariable Long templateId, @RequestBody Long[] countryIds) {
        MessageTemplate messageTemplate = manageMessageTemplateService.getById(templateId);
        LambdaQueryWrapper<MessageCountryTemplate> messageCountryTemplateLambdaQueryWrapper = Wrappers.lambdaQuery(MessageCountryTemplate.class)
                .eq(MessageCountryTemplate::getTemplateId, templateId);
        manageMessageCountryTemplateService.delete(messageCountryTemplateLambdaQueryWrapper);
        for (Long countryId : countryIds) {
            MessageCountryTemplate messageCountryTemplate = manageMessageCountryTemplateService.getByObj(Wrappers.lambdaQuery(MessageCountryTemplate.class).eq(MessageCountryTemplate::getCountryId, countryId).eq(MessageCountryTemplate::getTemplateCode, messageTemplate.getCode()));
            if (messageCountryTemplate != null) {
                throw new BaseException("一个国家不能绑定同一个类型模板多次");
            }
            messageCountryTemplate = new MessageCountryTemplate();
            messageCountryTemplate.setTemplateId(templateId);
            messageCountryTemplate.setCountryId(countryId);
            messageCountryTemplate.setTemplateCode(messageTemplate.getCode());
            manageMessageCountryTemplateService.save(messageCountryTemplate);
        }
        return AjaxResult.success();
    }

}
